from panda3d.core import *

loadPrcFileData("", "audio-library-name p3fmod_audio")
loadPrcFileData("", "basic-shaders-only #f")
#loadPrcFileData("", "dump-generated-shaders #t")
loadPrcFileData("", "fake-view-frustum-cull 0")
loadPrcFileData("", "sync-video 0")
from direct.showbase.ShowBase import ShowBase
from direct.task import Task 
from direct.gui.OnscreenText import OnscreenText
from direct.distributed.PyDatagram import PyDatagram
from direct.distributed.PyDatagramIterator import PyDatagramIterator
from panda3d.core import WindowProperties
from direct.actor.Actor import Actor
import random, sys, os, math
#mine
from Player import Player
from Events import Events
from Camera import Camera
from Client import Client
from Collisions import Physics
from Client import Client,NetworkedPlayer
import Map
from Gui import GUI
import Settings

class World(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
#        nout = MultiplexStream()
#        Notify.ptr().setOstreamPtr(nout, 0)
#        nout.addFile(Filename("Log.txt"))
        di = base.pipe.getDisplayInformation()
        l = di.getTotalDisplayModes()
        min = [di.getDisplayModeWidth(0),di.getDisplayModeHeight(0)]
        max = [di.getDisplayModeWidth(l-1),di.getDisplayModeHeight(l-1)]
        #create resoltuion mode list
        self.res = []
        self.bpp = []
        self.hz = []
        self.pause = False
        for i in range(l):
            if str([di.getDisplayModeWidth(i),di.getDisplayModeHeight(i)]) not in self.res:
                    self.res.append(str([di.getDisplayModeWidth(i),di.getDisplayModeHeight(i)]))
            if di.getDisplayModeRefreshRate(i) not in self.hz:
                self.hz.append(di.getDisplayModeRefreshRate(i))
            if di.getDisplayModeBitsPerPixel(i) not in self.bpp:
                self.bpp.append(di.getDisplayModeBitsPerPixel(i))
        self.settings = Settings.Load()
        if int(self.settings[1]) > max[0] or int(self.settings[2]) > max[1]:
            self.settings[1] = max[0]
            self.settings[2] = max[1]
        self.props = WindowProperties()
        self.props.setSize(int(self.settings[1]),int(self.settings[2]))
        self.props.setFullscreen(int(self.settings[3]))
        #self.props.setVerticalSync(int(self.settings[6]))
        base.win.requestProperties(self.props)
#        base.messenger.toggleVerbose()
#        print base.messenger
#        base.messenger.clear()
        self.Gui = GUI()
        self.Gui.MainMenuGui()

    def LoadGame(self,map,id):
        self.props.setCursorHidden(True)
        base.win.requestProperties(self.props)
        base.setFrameRateMeter(True)
        base.enableParticles()
        base.win.setClearColor(Vec4(0.0,0.0,0.0,1))
        self.Cam1 = Camera(self)
        self.Player1 = Player("Dylan")
        self.Player1.team = id
        self.Physics1 = Physics()
        self.event = Events(self)

        self.Player1.SetModel()
        self.Centre = self.event.findCenter()
        Map.LoadMap(self,map)
        Map.LoadLights(self)
        Map.Reflection(self)
        if int(self.settings[4]) == 1:
            Map.ShadowMapping(self)
        if int(self.settings[5]) == 1:
            Map.Bloom(self)
            self.taskMgr.add(Map.SkyScattering,"eventTask")
            base.skydome.setShader(Shader.load('shaders\Atmosphere\Sky.cg'))
        self.client = Client(self.settings[0])

        self.taskMgr.add(self.Physics1.Update, "Physics Simulation")
        self.taskMgr.add(self.event.handleEventGame,"eventTask")
        self.taskMgr.doMethodLater(1.0,Map.UpdateOne,"eventTask")

        #PStatClient.connect()
    def commands(self,task):
        self.commands = ["print","set","exit","load"]
        a = raw_input("Enter Command: ")
        b= a.split()
        if b[0] in self.commands:
            mycode = a
            try:
                exec mycode
            except:
                pass
                print("Invalid Data address.")
        else:
            print("Invaild Command")
        return Task.cont

w = World()
w.run()


